﻿namespace DotRas
{
    using DotRas.Design;
    using pptpclient.Properties;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Permissions;
    using System.Threading;

    [ToolboxItem(true), ToolboxBitmap(typeof(RasPhoneBookDialog), "DotRas.RasPhoneBookDialog.bmp")]
    public sealed class RasPhoneBookDialog : RasCommonDialog
    {
        private string _entryName;
        private string _phoneBookPath;
        private DotRas.NativeMethods.RasPBDlgFunc _rasPhonebookDlgCallback;

        [DotRas.SRCategory("CatBehavior"), DotRas.SRDescription("RPBDAddedEntryDesc")]
        public event EventHandler<EventArgs> AddedEntry;

        [DotRas.SRDescription("RPBDChangedEntryDesc"), DotRas.SRCategory("CatBehavior")]
        public event EventHandler<EventArgs> ChangedEntry;

        [DotRas.SRCategory("CatBehavior"), DotRas.SRDescription("RPBDDialedEntryDesc")]
        public event EventHandler<EventArgs> DialedEntry;

        [DotRas.SRDescription("RPBDRemovedEntryDesc"), DotRas.SRCategory("CatBehavior")]
        public event EventHandler<EventArgs> RemovedEntry;

        public RasPhoneBookDialog()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this._rasPhonebookDlgCallback = null;
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this._rasPhonebookDlgCallback = new DotRas.NativeMethods.RasPBDlgFunc(this.RasPhonebookDlgCallback);
        }

        private void OnAddedEntry(EventArgs e)
        {
            if (this.AddedEntry != null)
            {
                this.AddedEntry(this, e);
            }
        }

        private void OnChangedEntry(EventArgs e)
        {
            if (this.ChangedEntry != null)
            {
                this.ChangedEntry(this, e);
            }
        }

        private void OnDialedEntry(EventArgs e)
        {
            if (this.DialedEntry != null)
            {
                this.DialedEntry(this, e);
            }
        }

        private void OnRemovedEntry(EventArgs e)
        {
            if (this.RemovedEntry != null)
            {
                this.RemovedEntry(this, e);
            }
        }

        private void RasPhonebookDlgCallback(int callbackId, DotRas.NativeMethods.RASPBDEVENT eventType, string message, IntPtr data)
        {
            switch (eventType)
            {
                case DotRas.NativeMethods.RASPBDEVENT.AddEntry:
                    this.OnAddedEntry(EventArgs.Empty);
                    return;

                case DotRas.NativeMethods.RASPBDEVENT.EditEntry:
                    this.OnChangedEntry(EventArgs.Empty);
                    return;

                case DotRas.NativeMethods.RASPBDEVENT.RemoveEntry:
                    this.OnRemovedEntry(EventArgs.Empty);
                    return;

                case DotRas.NativeMethods.RASPBDEVENT.DialEntry:
                    this.OnDialedEntry(EventArgs.Empty);
                    return;
            }
        }

        public override void Reset()
        {
            this.PhoneBookPath = null;
            this.EntryName = null;
            base.Reset();
        }

        [UIPermission(SecurityAction.Demand, Window=UIPermissionWindow.SafeSubWindows)]
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            DotRas.NativeMethods.RASPBDLG lpInfo = new DotRas.NativeMethods.RASPBDLG {
                size = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASPBDLG)),
                hwndOwner = hwndOwner,
                callback = this._rasPhonebookDlgCallback,
                callbackId = IntPtr.Zero,
                reserved = IntPtr.Zero,
                reserved2 = IntPtr.Zero
            };
            if (base.Location != Point.Empty)
            {
                lpInfo.left = base.Location.X;
                lpInfo.top = base.Location.Y;
                lpInfo.flags |= DotRas.NativeMethods.RASPBDFLAG.PositionDlg;
            }
            bool flag = false;
            try
            {
                flag = DotRas.UnsafeNativeMethods.RasPhonebookDlg(this.PhoneBookPath, this.EntryName, ref lpInfo);
                if (!flag && (lpInfo.error != 0))
                {
                    base.OnError(new RasErrorEventArgs(lpInfo.error, RasHelper.GetRasErrorString(lpInfo.error)));
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            catch (SecurityException)
            {
                DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
            }
            return flag;
        }

        [DefaultValue((string) null), DotRas.SRCategory("CatData"), DotRas.SRDescription("RPBDEntryNameDesc")]
        public string EntryName
        {
            get
            {
                return this._entryName;
            }
            set
            {
                this._entryName = value;
            }
        }

        [DotRas.SRDescription("REDPhoneBookPathDesc"), DotRas.SRCategory("CatData"), DefaultValue((string) null)]
        public string PhoneBookPath
        {
            get
            {
                return this._phoneBookPath;
            }
            set
            {
                this._phoneBookPath = value;
            }
        }
    }
}

